# $Id$
# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=flake8
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=3.8.4
pkgrel=1
pkgdesc="The modular source code checker: pep8, pyflakes and co"
arch=('any')
url="http://flake8.pycqa.org"
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python-pyflakes"
         "${MINGW_PACKAGE_PREFIX}-python-mccabe"
         "${MINGW_PACKAGE_PREFIX}-python-pycodestyle")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-pytest-runner")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-mock")
install=${_realname}3-${CARCH}.install
source=("${_realname}-$pkgver.tar.gz"::"https://github.com/PyCQA/flake8/archive/${pkgver}.tar.gz")
sha512sums=('de2e22c2fa759870efe877e70a28a7b831648c719fe0ec2df953667932c9ff191d9268094454d0c8d9ed00235dd75a17166efebe5b7417f71c1b38207a4c5962')

prepare() {
  # disable overly restrictive dependency pinning
  sed -i -e 's/, *< *[0-9=.]*//' flake8-${pkgver}/setup.py

  rm -rf python-build-${CARCH}| true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

build() {
  msg "Python build for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  msg "Python test for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py pytest
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --root="${pkgdir}" --prefix=${MINGW_PREFIX} --optimize=1
  install -D -m644 LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/LICENSE

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}
